package com.cmict.data.infra.dal.mysql.message;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cmict.data.framework.common.pojo.PageResult;
import com.cmict.data.framework.mybatis.core.mapper.BaseMapperX;
import com.cmict.data.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.cmict.data.infra.controller.admin.vo.MessageTemplateExportReqVO;
import com.cmict.data.infra.controller.admin.vo.MessageTemplatePageReqVO;
import com.cmict.data.infra.dal.dataobject.message.Announcement;
import com.cmict.data.infra.dal.dataobject.message.MessageTemplateDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 信息模板 Mapper
 *
 * @author liutao
 * @date 2022/5/11
 */
@Mapper
public interface MessageTemplateMapper extends BaseMapperX<MessageTemplateDO> {

    default PageResult<MessageTemplateDO> selectPage(MessageTemplatePageReqVO reqVO) {
        QueryWrapper<MessageTemplateDO> queryWrapper=new QueryWrapper<>();
        if(reqVO.getValue()!=null){
            queryWrapper.lambda().and(qw -> qw.like(MessageTemplateDO::getTitle, reqVO.getValue())
                    .or().like(MessageTemplateDO::getContentDisplay, reqVO.getValue()));
        }
        queryWrapper.lambda().in(reqVO.getStatusList() != null && reqVO.getStatusList().size()>0, MessageTemplateDO::getStatus, reqVO.getStatusList())
                .in(reqVO.getTypes()!=null && reqVO.getTypes().size()>0,MessageTemplateDO::getType,reqVO.getTypes())
//                .orderBy(reqVO.getSortord() != null ,reqVO.getSortord().equals("ASC"),MessageTemplateDO::getCreateTime);
        .orderBy(true ,reqVO.getSortord()!=null && reqVO.getSortord().equals("ASC"),MessageTemplateDO::getCreateTime);
        return selectPage(reqVO,queryWrapper);
    }

    default List<MessageTemplateDO> selectList(MessageTemplateExportReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<MessageTemplateDO>()
                .eqIfPresent(MessageTemplateDO::getType, reqVO.getType())
                .eqIfPresent(MessageTemplateDO::getTitle, reqVO.getTitle())
                .eqIfPresent(MessageTemplateDO::getContent, reqVO.getContent())
                .eqIfPresent(MessageTemplateDO::getChannel, reqVO.getChannel())
                .eqIfPresent(MessageTemplateDO::getStatus, reqVO.getStatus())
                .betweenIfPresent(MessageTemplateDO::getCreateTime, reqVO.getBeginCreateTime(), reqVO.getEndCreateTime())
                .orderByDesc(MessageTemplateDO::getId));
    }
    @Select({"select id from `sys_message_template` where title = #{title}  order by create_time asc limit 1"})
    Long getTemplateByTitle(String title);
}
